<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

interface UserProfile {
  name: string
  email: string
  phone: string
  avatar: string
}

const userProfile = ref<UserProfile>({
  name: '张三',
  email: 'zhangsan@example.com',
  phone: '13812345678',
  avatar: '👤'
})

const isEditing = ref(false)

onMounted(() => {
  // 检查是否是普通用户
  const userRole = sessionStorage.getItem('userRole')
  if (userRole !== 'user') {
    router.push('/404')
  }
})

function goBack() {
  router.push('/user')
}

function toggleEdit() {
  isEditing.value = !isEditing.value
}

function saveProfile() {
  // 这里是模拟保存操作
  isEditing.value = false
  alert('个人资料已更新')
}
</script>

<template>
  <div class="user-profile">
    <header class="header">
      <button @click="goBack" class="back-btn">返回</button>
      <h1>个人资料</h1>
    </header>
    
    <div class="content">
      <div class="profile-container">
        <div class="profile-header">
          <div class="avatar">{{ userProfile.avatar }}</div>
          <div class="actions">
            <button @click="toggleEdit" class="action-btn">
              {{ isEditing ? '取消' : '编辑资料' }}
            </button>
            <button v-if="isEditing" @click="saveProfile" class="action-btn primary">
              保存
            </button>
          </div>
        </div>
        
        <div class="profile-details">
          <div class="profile-row">
            <div class="label">姓名</div>
            <div class="value">
              <input v-if="isEditing" v-model="userProfile.name" type="text" />
              <span v-else>{{ userProfile.name }}</span>
            </div>
          </div>
          
          <div class="profile-row">
            <div class="label">电子邮箱</div>
            <div class="value">
              <input v-if="isEditing" v-model="userProfile.email" type="email" />
              <span v-else>{{ userProfile.email }}</span>
            </div>
          </div>
          
          <div class="profile-row">
            <div class="label">电话</div>
            <div class="value">
              <input v-if="isEditing" v-model="userProfile.phone" type="tel" />
              <span v-else>{{ userProfile.phone }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.user-profile {
  min-height: 100vh;
  background-color: #f0f2f5;
}

.header {
  display: flex;
  align-items: center;
  padding: 1rem 2rem;
  background-color: #42b883;
  color: white;
}

.back-btn {
  padding: 0.5rem 1rem;
  background-color: rgba(255, 255, 255, 0.2);
  border: 1px solid white;
  color: white;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 1rem;
  transition: background-color 0.3s;
}

.back-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.content {
  max-width: 800px;
  margin: 2rem auto;
  padding: 0 1rem;
}

.profile-container {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.profile-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2rem;
  background-color: #f9f9f9;
  border-bottom: 1px solid #eee;
}

.avatar {
  font-size: 4rem;
  background-color: #e1e1e1;
  width: 100px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.actions {
  display: flex;
  gap: 1rem;
}

.action-btn {
  padding: 0.5rem 1.5rem;
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.action-btn:hover {
  border-color: #42b883;
  color: #42b883;
}

.action-btn.primary {
  background-color: #42b883;
  color: white;
  border-color: #42b883;
}

.action-btn.primary:hover {
  background-color: #33a06f;
}

.profile-details {
  padding: 2rem;
}

.profile-row {
  display: flex;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 1rem;
}

.profile-row:last-child {
  margin-bottom: 0;
  border-bottom: none;
}

.label {
  width: 100px;
  font-weight: 600;
  color: #666;
}

.value {
  flex-grow: 1;
}

input {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
}

input:focus {
  outline: none;
  border-color: #42b883;
}
</style> 